package com.sttk.fastMark.camera.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.fastMark.camera.R;
import com.sttk.fastMark.camera.adapter.ColorAdapter;
import com.sttk.fastMark.camera.basic.dialog.BaseSmartDialog;
import com.sttk.fastMark.camera.bean.ColorBean;
import com.sttk.fastMark.camera.bean.WorkReportBean;
import com.sttk.fastMark.camera.databinding.DialogEditTemplate1Binding;
import com.sttk.fastMark.camera.utils.AppSpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class EditTemplate1Dialog extends BaseSmartDialog<DialogEditTemplate1Binding> implements View.OnClickListener, OnItemClickListener {

    private WorkReportBean workReportBean;
    private ColorAdapter colorAdapter;
    private List<ColorBean> listColor;
    private int colorPos;
    private SelTemplateDateDialog selTemplateDateDialog;

    public EditTemplate1Dialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_edit_template1;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.tvDate.setOnClickListener(this);
        mDataBinding.ivShowSubtitle.setOnClickListener(this);
        mDataBinding.ivShowDate.setOnClickListener(this);
        mDataBinding.ivShowReporter.setOnClickListener(this);
        mDataBinding.ivShowWorkSummary.setOnClickListener(this);

        mDataBinding.rvColorList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);

        workReportBean = AppSpUtils.getWorkReport();
        mDataBinding.etTitle.setText(workReportBean.getTitle());
        mDataBinding.etSubtitle.setText(workReportBean.getSubtitle());
        mDataBinding.etReporter.setText(workReportBean.getReporter());
        mDataBinding.etWorkSummary.setText(workReportBean.getWorkSummary());

        if (TextUtils.isEmpty(workReportBean.getDate())) {
            mDataBinding.tvDate.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
        } else {
            mDataBinding.tvDate.setText(workReportBean.getDate());
        }

        mDataBinding.ivShowSubtitle.setImageResource(workReportBean.isShowSubtitle() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowDate.setImageResource(workReportBean.isShowDate() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowReporter.setImageResource(workReportBean.isShowReporter() ? R.drawable.akai : R.drawable.aguan);
        mDataBinding.ivShowWorkSummary.setImageResource(workReportBean.isShowWorkSummary() ? R.drawable.akai : R.drawable.aguan);

        listColor = new ArrayList<>();
        listColor.add(new ColorBean("#000000", false));
        listColor.add(new ColorBean("#D84545", false));
        listColor.add(new ColorBean("#FFA600", false));
        listColor.add(new ColorBean("#FAFD00", false));
        listColor.add(new ColorBean("#13FD00", false));
        listColor.add(new ColorBean("#00F5FF", false));
        listColor.add(new ColorBean("#4B6DFF", false));
        listColor.add(new ColorBean("#FF29FD", false));

        for (int i = 0; i < listColor.size(); i++) {
            ColorBean colorBean = listColor.get(i);
            if (colorBean.getColor().equals(workReportBean.getThemeColor())) {
                colorPos = i;
                colorBean.setSelected(true);
                break;
            }
        }
        colorAdapter.setList(listColor);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            saveData();
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }
        } else if (id == R.id.tvDate) {//日期
            showSelDateDialog();
        } else if (id == R.id.ivShowSubtitle) {//显示小标题
            workReportBean.setShowSubtitle(!workReportBean.isShowSubtitle());
            mDataBinding.ivShowSubtitle.setImageResource(workReportBean.isShowSubtitle() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowDate) {//显示日期
            workReportBean.setShowDate(!workReportBean.isShowDate());
            mDataBinding.ivShowDate.setImageResource(workReportBean.isShowDate() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowReporter) {//显示汇报人
            workReportBean.setShowReporter(!workReportBean.isShowReporter());
            mDataBinding.ivShowReporter.setImageResource(workReportBean.isShowReporter() ? R.drawable.akai : R.drawable.aguan);
        } else if (id == R.id.ivShowWorkSummary) {//显示工作总结
            workReportBean.setShowWorkSummary(!workReportBean.isShowWorkSummary());
            mDataBinding.ivShowReporter.setImageResource(workReportBean.isShowWorkSummary() ? R.drawable.akai : R.drawable.aguan);
        }
    }

    private void saveData() {
        String title = mDataBinding.etTitle.getText().toString();
        String date = mDataBinding.tvDate.getText().toString();
        String subtitle = mDataBinding.etSubtitle.getText().toString();
        String reporter = mDataBinding.etReporter.getText().toString();
        String workSummary = mDataBinding.etWorkSummary.getText().toString();

        if (TextUtils.isEmpty(title)) {
            title = "";
        }
        if (TextUtils.isEmpty(subtitle)) {
            subtitle = "";
        }
        if (TextUtils.isEmpty(reporter)) {
            reporter = "";
        }
        if (TextUtils.isEmpty(workSummary)) {
            workSummary = "";
        }

        workReportBean.setTitle(title);
        workReportBean.setSubtitle(subtitle);
        workReportBean.setDate(date);
        workReportBean.setReporter(reporter);
        workReportBean.setWorkSummary(workSummary);

        AppSpUtils.saveWorkReport(workReportBean);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(colorPos).setSelected(false);
        colorPos = position;
        colorAdapter.getItem(position).setSelected(true);
        colorAdapter.notifyDataSetChanged();
        workReportBean.setThemeColor(colorAdapter.getItem(position).getColor());
    }

    private void showSelDateDialog() {
        if (selTemplateDateDialog == null) {
            selTemplateDateDialog = new SelTemplateDateDialog(getContext());
            selTemplateDateDialog.setDate(mDataBinding.tvDate.getText().toString());
        }
        selTemplateDateDialog.setListener(new SelTemplateDateDialog.Ilistener() {
            @Override
            public void clickConfirm(long date) {
                mDataBinding.tvDate.setText(TimeUtils.millis2String(date, new SimpleDateFormat("yyyy-MM-dd")));
            }
        });
        selTemplateDateDialog.show();
    }


    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}
